//index.js
//获取应用实例
var app = getApp();
var _openid = app.getOpenId();
var goPayUrl = app.globalData.domain + app.globalData.subDomain +'/weixin/goPay';

Page({
  data: {
    coupons:[],
    orderId : 0
  },
  onLoad: function (e) {
    this.getMyCoupons(e.id);
  },
 
  onShow : function (e) {
    
  },
  getMyCoupons: function (e) {
    var that = this;
    that.setData({
      orderId : e
    });
    console.log('订单详情的ID' +e);
    wx.request({
      url: app.globalData.domain + app.globalData.subDomain +'/order/detail?',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        id:e,
        openid:_openid,
      },
      success: function (res) {
        var coupons =[];
        wx.hideLoading();
        if (res.data.code == 100) {
          coupons.push(res.data.obj);
          that.setData({
            coupons:coupons,
          });
        } else {
          coupons:[];       

      }
      }
    })
  },
  goBuy:function(){
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },


  goPay: function (e) {
    var that = this;
    var orderId = that.data.orderId;
    console.log("xx"+orderId);
    //发起网络请求
    wx.request({
      url: goPayUrl,
      data: {
        id: orderId,
        openid: _openid
      },
      success: function (res) {
        console.log(res);
        // return;
        if (null != res.data.obj) {
          wx.requestPayment({
            'timeStamp': res.data.obj.timeStamp,
            'nonceStr': res.data.obj.nonceStr,
            'package': res.data.obj.package,
            'signType': res.data.obj.signType,
            'paySign': res.data.obj.paySign,
            'success': function (res) {
              console.log(res);
              if (res.errMsg == "requestPayment:ok") {
                console.log("支付成功");
              } else {
                console.log("支付失败");
              }
            },
            'fail': function (res) {
              console.log(res);
            }
          })
        }

      }

    })
  }


  

})
